﻿using System;
using System.Globalization;
using System.Net;
using PMC.Interfaces;
using PMC.Plugins;
using PMC.Structs;

namespace PMC.Scripts
{
    public abstract class Script : ScriptInfo, IHandleable
    {
        private static Script defaultScript;
        /// <summary>
        /// Script for unknown websites
        /// </summary>
        public static Script Default
        {
            get
            {
                return defaultScript ??
                       (defaultScript = Plugin<Script>.FindPlugin(Properties.Settings.Default.NoWebsiteFound).Instance);
            }
            set
            {
                if (value == null)
                    throw new ArgumentNullException("value");
                Properties.Settings.Default.NoWebsiteFound = value.GetType().ToString();
                defaultScript = value;
            }
        }

        private int interval = 10000;
        /// <summary>
        /// Interval to wait for navigating to next page.
        /// <br/>
        /// <exception cref="Interval">Interval must be greater than 0</exception>
        /// </summary>
        public int Interval
        {
            get
            {
                return interval;
            }
            internal protected set
            {
                if (value <= 0)
                    throw new ArgumentException("Interval must be greater than 0");
                interval = value;
            }
        }

        //private readonly string _path = Properties.Settings.Default.SettingsFolder + @"\PMC.Script.O{0}.xml";

        public abstract HtmlDocumentHandler Handle(IWbrowser webBrowser);
        public abstract void Login(Website website, string path = null);

        public string Name
        {
            get
            {
                var culture = GetScriptInfoByCulture(CultureInfo.CurrentCulture); //Todo invariant
                return culture.Title;
            }
        }

        public override string ToString()
        {
            return Name;
        }
    }
}
